<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <!-- creating function -->
    <script>
        /**
         * 计算产品税额
         * @param {string} product_category 产品类别
         * @param {string} product_name 产品名称
         * @param {number} price 产品价格
         * @returns {number} 税额
         */
        function tax(product_category, product_name, price) {
            var total_price = undefined;
            if (product_category == 'Mobile') {
                total_price = (12.5 / 100) * price + price;
            } else if (product_category == 'Medicine') {
                total_price = price
            }
            console.log('category = ' + product_category + ', name = ' + name + ', price = ' + price + ', tax = ' + total_price);
        }
    </script>

    <!-- calling a function -->
    <script>
        var product_category = 'Medicine';
        tax(product_category, 'Paracetamol', 8000);

        var product_category = 'Mobile';
        tax(product_category, 'GV3', 3000);
        tax(product_category, 'XV5', 5000);
    </script>

    <!-- returning values from function -->
    <script>
        /**
          * 求和
          * @param {number} a 第一个操作数
          * @param {number} b 第二个操作数
          * @returns {number} a和b的和
          */
        function sum(a, b) {
            return a + b;
        }

        var a = 3;
        var b = 6;
        var total = sum(a, b);
        console.log('a = ' + a + ', b = ' + b + ', sum = ' + total);
        document.writeln('a = ' + a + ', b = ' + b + ', sum = ' + total);
    </script>

</body>

</html>

<!-- 函数-可复用代码块 -->
<!-- https://developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Building_blocks/Functions -->

<!-- 创建您自己的函数 -->
<!-- https://developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Building_blocks/Build_your_own_function -->

<!-- 函数返回值 -->
<!-- https://developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Building_blocks/Return_values -->

<!-- function -->
<!-- https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/function -->